//例子中使用requireAuth来表示是否需要检查
//router.js 配置路由的文件
//引入路由
import Vue from 'vue';
import Router from "vue-router"
Vue.use(Router);


import HelloWorld from '@/components/HelloWorld'
import outshop_lzy from '@/page/outshop_lzy'
import inshop_lzy from '@/page/inshop_lzy'
import shoptype_bdl from '@/page/shoptype_bdl'
import shop from '@/page/shop_bdl.vue'
import dao from '@/page/dao_quan.vue'
import echarts from '@/page/echarts_quan.vue'
import uservue from '@/page/usermanage_quan.vue'
import usercen from '@/page/usercen_quan.vue'
import login from '@/page/login_zyc.vue'
import add from '@/page/add_zyc.vue'



let router = new Router({
	mode: 'history',
	routes: [{
			path: '/',
			name: 'login',
			component: login
		},
		{
			path: '/dao',
			name: 'dao',
			component: dao,
			meta: {
				requireAuth: true
			},
			children: [{
					path: '/inshop',
					name: 'inshop',
					component: inshop_lzy
				},
				{
					path: '/shoptype',
					name: 'shoptype',
					component: shoptype_bdl
				},
				{
					path: '/shop',
					name: 'shop',
					component: shop
				},
				{
					path: '/outshop',
					name: 'outshop',
					component: outshop_lzy
				},
				{
					path: '/echarts',
					name: 'echarts',
					component: echarts
				},
				{
					path: '/uservue',
					name: 'uservue',
					component: uservue
				},
				{
					path: '/usercen',
					name: 'usercen',
					component: usercen
				}
			]

		},
	

		{
			path: '/login',
			name: 'login',
			component: login
		},
		{
			path: '/add',
			name: 'add',
			component: add
		}
	]
})

export default router
//还是在router.js中 我们这里就放在 export default router 的后面
//登录拦截
router.beforeEach((to, from, next) => {
	if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
		//如果需要就执行下面的代码

		var num = sessionStorage.getItem("isLogin");
		// 通过sessionStorage 获取当前的isLogin的值 将我们保存的isLogin的值赋给num,num是顺便取的名称，可以换
		//我们在登录界面，我们使用请求，请求成功后，我们就使用sessionStorage为‘isLogin’保存一个值  1，如果请求失败，就不保存‘isLogin’的值
		//如果请求成功，num的值就是1，请求失败就是null，所以下面进行判断
		if (num == 1) {
			//如果登录了，就跳转到'/index'路径
			next();
		} else {
			next({
				path: '/login', //返回登录界面
				// query: {redirect: to.fullPath}  
			})
		}
	} else {
		//如果不需要登录权限就直接跳转到该路由
		next();
	}
})
